package com.retired_serviceman_management.service.impl;

import com.retired_serviceman_management.dao.EnterpriseInfoDao;
import com.retired_serviceman_management.entity.EnterpriseInfo;
import com.retired_serviceman_management.service.Enterprise_Info_Service;
import com.retired_serviceman_management.util.Enterprise_util;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;

@Service
public class Enterprise_Info_Service_Impl implements Enterprise_Info_Service {

    @Resource
    private EnterpriseInfoDao enterpriseInfoDao;

    @Override
    public List<EnterpriseInfo> queryAll() {
        List<EnterpriseInfo> enterpriseInfos = enterpriseInfoDao.queryAll();
        if (enterpriseInfos != null){
            for(int i =0;i<enterpriseInfos.size();i++){
                String status = enterpriseInfos.get(i).getEnStatus();
                if(status!=null&&status.equals("I")){
                    enterpriseInfos.get(i).setEnStatus("未通过");
                }
                else if(status!=null&&status.equals("A")){
                    enterpriseInfos.get(i).setEnStatus("已通过");
                }
            }
        }
        return enterpriseInfos;
    }

    @Override
    public Integer Update_Enterprise(EnterpriseInfo enterpriseInfo) {
        EnterpriseInfo enterpriseInfo1 = enterpriseInfoDao.queryLimitBycomAccount(enterpriseInfo.getComAccount());
        if((enterpriseInfo.getComId().equals(enterpriseInfo1.getComId()))&&(enterpriseInfo.getComName().equals(enterpriseInfo1.getComName()))&& enterpriseInfo.getComLocal().equals(enterpriseInfo1.getComLocal())&&enterpriseInfo.getRecruitmentRequirements().equals(enterpriseInfo1.getRecruitmentRequirements())&&
            enterpriseInfo.getRecruitmentPositions().equals(enterpriseInfo1.getRecruitmentPositions())&&enterpriseInfo.getSalaryRange().equals(enterpriseInfo1.getSalaryRange())&&enterpriseInfo.getComAccount().equals(enterpriseInfo1.getComAccount())){
            return 2;
        }
        int i = enterpriseInfoDao.update(enterpriseInfo);
        return i;
    }

    @Override
    public Integer insert_Enterprise(Enterprise_util enterpriseInfo) {
        if(enterpriseInfo==null) return 2;
        EnterpriseInfo enterpriseInfo1 = enterpriseInfoDao.queryBycomAccount(enterpriseInfo.getComAccount());
        if(enterpriseInfo1!=null) return 3;
        EnterpriseInfo enterpriseInfo2 = new EnterpriseInfo(enterpriseInfo.getComName(),enterpriseInfo.getComLocal(),enterpriseInfo.getRecruitmentRequirements(),enterpriseInfo.getRecruitmentPositions(),enterpriseInfo.getSalaryRange(),enterpriseInfo.getComAccount(),enterpriseInfo.getComPassword());
        int i = enterpriseInfoDao.insert(enterpriseInfo2);
        return i;
    }

    @Override
    public Integer Delete_Enterprise(Integer comAccount) {
        Integer i = enterpriseInfoDao.Delete_Enterprise(comAccount);
        return i;
    }
    @Override
    public Integer CheckAccount(String comAccount,String comPassword) {
        EnterpriseInfo enterpriseInfo1 = enterpriseInfoDao.queryBycomAccount(comAccount);
        System.out.println("soldierInfo = " + enterpriseInfo1);
        if(enterpriseInfo1 == null){
            return 1;
        }
        else {
            if(enterpriseInfo1.getComPassword().equals(comPassword)){
                return 0;
            }
            else return 2;
        }
    }
    @Override
    public EnterpriseInfo queryBycomAccount(String comAccount) {
        EnterpriseInfo enterpriseInfo = enterpriseInfoDao.queryBycomAccount(comAccount);
            String status = enterpriseInfo.getEnStatus();
            if(status!=null&&status.equals("I")){
                enterpriseInfo.setEnStatus("未通过");
            }
            else if(status!=null&&status.equals("A")){
                enterpriseInfo.setEnStatus("已通过");
            }
        EnterpriseInfo enterpriseInfo1 = (EnterpriseInfo) checkObjAllFieldsIsNull(enterpriseInfo);//如果属性为空，使其为“ ”
        return enterpriseInfo1;
    }

    @Override
    public Integer register(String comName, String comAccount, String comPassword, String comPassword1) {
        if (comPassword.equals(comPassword1)) {//两次输入的密码相等
            EnterpriseInfo enterpriseInfo = enterpriseInfoDao.queryBycomAccount(comAccount);
            if (enterpriseInfo != null) {
                return 2;//账号已经存在，无法注册
            } else {
                EnterpriseInfo enterpriseInfo1 = new EnterpriseInfo();
                enterpriseInfo1.setComName(comName);
                enterpriseInfo1.setComAccount(comAccount);
                enterpriseInfo1.setComPassword(comPassword);
                int i = enterpriseInfoDao.insert(enterpriseInfo1);
                return i;//0 失败  1成功
            }
        }
        return 3;//两次输入密码不同
    }
    @Override
    public Integer Update_password(String comAccount,String old_password,String new_password,String new_password2) {
        if(new_password.equals(new_password2)){//两次输入的新密码一致
            String password = enterpriseInfoDao.queryPasswordByAccount(comAccount);
            if(password.equals(old_password)){//输入的旧密码和数据库一致，可以修改
                Integer integer = enterpriseInfoDao.updatePasswordBycomAccount(comAccount, new_password);
                return integer;//1修改成功  0修改失败
            }
            else return 2;//输入的旧密码错误
        }
        else return 3;//两次输入的密码不一致
    }
    public static Object checkObjAllFieldsIsNull(Object object) {
        try {
            for (Field f : object.getClass().getDeclaredFields()) {
                if(f.get(object)==null){
                    f.set(object," ");
                }
            }
            return object;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }
}
